extends Node

@export var mob_scene: PackedScene
var score
# Called when the node enters the scene tree for the first time.
func _ready():
	pass

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	pass



func game_over():
	$ScoreTimer.stop()
	$MobTimer.stop()
	$HUD.show_game_over()
	
func new_game():
	score = 0
	$Player.start($StartPosition.position)
	$StartTimer.start()
	
	$HUD.update_score(score)
	$HUD.show_message("Get Ready")
	#queue_free讲标签mobs的怪在执行结束后自动回收
	get_tree().call_group("mobs", "queue_free")

func _on_mob_timer_timeout():
	#把怪物实例化
	var mob = mob_scene.instantiate()
	#沿着mobPath画好的路径，随机选取位置放置
	var mob_spawn_location = get_node("MobPath/MobSpawnLocation")
	mob_spawn_location.progress_ratio = randf()
	mob.position = mob_spawn_location.position
	#随机旋转
	var direction = mob_spawn_location.rotation + PI / 2
	direction += randf_range(-PI / 4, PI / 4)
	mob.rotation = direction
	
	#给怪物移动速度使它开始移动，y=0表示只有水平方向的速度为150-250
	var velocity = Vector2(randf_range(150.0, 250.0), 0.0)
	#控制方向按照圆来旋转
	mob.linear_velocity = velocity.rotated(direction)
	
	# 添加到场景
	add_child(mob)


func _on_score_timer_timeout():
	score += 1
	$HUD.update_score(score)


func _on_start_timer_timeout():
	$MobTimer.start()
	$ScoreTimer.start()
